package concurent

import (
	"fmt"
	"time"
)

/*
	ticker周期内执行

*/

func Ticker_f1() {
	t := time.NewTicker(time.Second * 2)
	count := 1
	for _ = range t.C {
		fmt.Println("ticker")
		count++
		if count >= 5 {
			t.Stop()
			break
		}
	}
}

func Ticker_f2() {
	chanInt := make(chan int)
	ticker := time.NewTicker(time.Second)
	go func() {
		for _ = range ticker.C {
			select {
			case chanInt <- 1:
			case chanInt <- 2:
			case chanInt <- 3:
			}
		}
	}()

	sum := 0
	for v := range chanInt {
		fmt.Printf("recieve v: %v\n", v)
		sum += v
		if sum >= 10 {
			ticker.Stop()
			break
		}
	}

}
